import axios from 'axios'
import { Message,MessageBox} from 'element-ui'
import store from '../store'

//请求地址
axios.defaults.baseURL = 'http://127.0.0.1:8998'

//设置超时时间
axios.defaults.timeout = 50000
// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.withCredentials = true


// 响应拦截器（处理响应数据）
axios.interceptors.response.use(
    (res) => {
        const ret = res.data
        if(ret.status !== 200){
            if(ret.status == 101){
                MessageBox.confirm(ret.msg, '系统提示', {
                    confirmButtonText: '重新登录',
                    cancelButtonText: '取消',
                    type: 'warning'
                  }
                ).then(() => {
                    store.dispatch('logout').then(() => {
                        location.href = '/';
                    })
                })
            }else{
                Message({
                    showClose: true,
                    message: ret.msg || 'Server error',
                    type: 'error',
                    duration: 2000
                })
            }
            reject(ret.msg)
        }
        return res
    }, 
    (error) => {
        Message({
            showClose: true,
            message: error.message,
            type: 'error',
            duration: 2000
        })
        return Promise.reject(error);
    }
);
